package com.glory.dgcpcpda.activity.recipe;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.UpdateIPEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class SealDownLoadActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.tvCustomer)
    TextView tvCustomer;
    @BindView(R.id.tvOrderNum)
    TextView tvOrderNum;
    @BindView(R.id.tvSealFile)
    TextView tvSealFile;
    @BindView(R.id.tvDownLoad)
    TextView tvDownLoad;
    @BindView(R.id.tvBiaoID)
    TextView tvBiaoID;
    @BindView(R.id.tvAssOrderNum)
    TextView tvAssOrderNum;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private LotInfoEntity lotInfoEntity;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_seal_down_load;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvBaseSubmit.setVisibility(View.GONE);
        tvTitle.setText("印章调取");

        initEdit(etEqpID, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                    (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        UpdateIPEntity body = response.body().getResponse().getBody();
                        for (UpdateIPEntity.AdrefList data:body.getADREFLIST()){
                            FreeApi_LogUtils.saveErrorMessages(data.getTEXT(),"UpdateIPEntity");
                            if (s.equals(data.getTEXT())){
                                tvBiaoID.setText(data.getKEY());
                                getData(data.getKEY());
                            }
                        }
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "CPC.ADREFLISTQUERY");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("REFERENCETYPE","USER");
            map.addProperty("REFERENCENAME",s);
            NetUtils.requestPostNet("CPC.ADREFLISTQUERY","GETEQPTFTLS", SealDownLoadActivity.this,map,callBack);

        });

        initEdit(etLot, s -> {
            if (getText(etEqpID).equals("")){
                showToast("请先扫描设备号",false);
                etLot.setText("");
                etEqpID.requestFocus();
                return;
            }

            LotUtils.searchLotInfo(SealDownLoadActivity.this, s, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
                @Override
                public void onSuccess(LotInfoEntity response) {
                    lotInfoEntity = response;
                    tvCustomer.setText(response.getLOT().getCUSTOMERCODE());
                    tvOrderNum.setText(response.getLINEID());
                    tvAssOrderNum.setText(response.getASSORDER());
                    requestSealInfo(response);
                }

                @Override
                public void onFail(Throwable t) {
                    loadError(t,"LOTQUERY");
                }
            });

        });
    }

    private void getData(String s) {
        FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    UpdateIPEntity body = response.body().getResponse().getBody();
                    tvEqpType.setText(body.getEQPTYPE());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GETEQPINFOBYEQPID");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQPID",s);
        NetUtils.requestPostNet("EAP.GETEQPINFOBYEQPID","GETEQPINFOBYEQPID", SealDownLoadActivity.this,map,callBack);

    }

    private void requestSealInfo(LotInfoEntity response) {
        FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    tvSealFile.setText(response.body().getResponse().getBody().getMARKPATH());
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"GetEqpMarkNumPath");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("CUSTOMERCODE",response.getLOT().getCUSTOMERCODE());
        map.addProperty("LINEID",response.getLINEID());
        map.addProperty("EqpID",getText(etEqpID));
        map.addProperty("PROGRAM_TYPE",tvEqpType.getText().toString());
        map.addProperty("assOrder",tvAssOrderNum.getText().toString());
        NetUtils.requestPostNet("GetEqpMarkNumPath","GetEqpMarkNumPath",this,map,callBack);
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.tvDownLoad)
    public void onViewClicked() {
        if (actionControl("DownLoad05")) {
            return;
        }

        if (getText(etLot).equals("") || getText(etEqpID).equals("")){
            showToast("请先扫描批次号或设备号",false);
            return;
        }
        if (lotInfoEntity == null){
            showToast("Lot数据为空",false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    showConfirmDialog(SealDownLoadActivity.this,"调用成功!");
                }


            }

            @Override
            public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"SendEqpMarkNum");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQPID",tvBiaoID.getText().toString());
        map.addProperty("LOTNO",getText(etLot));
        map.addProperty("HDLEQPID",getText(etEqpID));
        map.addProperty("LOTCount",lotInfoEntity.getLOT().getMAINQTY());
        map.addProperty("PACKAGE",lotInfoEntity.getPACKAGETYPE());
        map.addProperty("FILEPATH",tvSealFile.getText().toString());
        map.addProperty("assOrder",tvAssOrderNum.getText().toString());
        NetUtils.requestPostNet("CPC.SendEqpMarkNum","SendEqpMarkNum",this,map,callBack);
    }
}